﻿var Promise = require('bluebird');
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var crypto = require('crypto');
var Users = require('../Module/User');

'use strict'

var exportCtller = {};

exportCtller.EditUserInfo = function (row, callback) {
    Users.update({ _id: row._id }, {
        employee_no: row.emp_no,
        employee_name: row.emp_name,
        department_name: row.dpt_name,
        position: row.position
    }, function (err, doc) {
        if (err) { callback(err, null); }
        else {
            Users.findOne({ _id: row._id }, callback);
        }
    });
};

exportCtller.EditLoginPSW = function (row, callback) {
    // 校验原始密码是否正确
    Users.findOne({ _id: row._id }, function (err, user) {
        user.comparePassword(row.old_psw, function (err, ismatch) {
            if (err) { callback(err, null); }
            if (ismatch) {
                bcrypt.genSalt(10, function (err, salt) {
                    if (err) { return callback(err,null); }
                    bcrypt.hash(row.password, salt, null, function (err, hash) {
                        if (err) { return callback(err, null); }
                        Users.update({ _id: row._id },{ psw: hash }, callback);
                    });
                });                
            }
            else { callback(new Error("密码不正确!"), null) }
        });
    });
};

exportCtller.GetLoginUserInfo = function (_id, callback) {
    
};

module.exports = exportCtller;